<?php
namespace Admin\Controller;

use Think\Controller;

class UserController extends Controller
{
    public function index()
    {
        if (isMobile()) {
            $this->display("index_bf");
        } else {
            $this->display();
        }
    }

//后台登录(商家)
    public function reg()
    {
        if (!IS_AJAX) {
            $this->error("非法请求");
        } else {
            $phone = $_POST['phone'];
            $pwd = $_POST['pwd'];
            $role = $_POST['role'];
            $obj = M("user_main");
            $arr = $obj->where("phone=" . $phone)->find();
            if (empty($arr)) {
                $data['code'] = 2002;//账号不存在，请注册
                $this->ajaxReturn($data);
            } else {
                $res2 = $obj->where("phone=" . $phone . " and role=" . $role)->find();
                if (empty($res2)) {
                    $data['code'] = 2003;//您不是商家，无法登陆
                    $this->ajaxReturn($data);
                }
                $res3 = $obj->where("phone=" . $phone . " and pwd='" . $pwd . "' and role=" . $role)->find();
                if (empty($res3)) {
                    $data['code'] = 2004;//密码错误
                    $this->ajaxReturn($data);
                }
                session("user", $phone);
                session("role", $res3["role"]);
                session("uid", $res3["uid"]);
                cookie("user", $phone);
                cookie("user", $phone, 60);
                $data['code'] = 2001;//登陆成功
                $this->ajaxReturn($data);
            }
        }
    }

    public function lt($phone)
    {
        $record = M("user_record");
        $datas = $record->where("phone=" . $phone)->find();
        $belong = $datas["belong"];
        if ($belong != null) {
            $umain = M("user_main");
            $uprofile = M("user_profile");
            $arr = $umain->where("phone=" . $phone)->find();
            $uid = $arr["uid"];
            $c_arr = $uprofile->where("type=1 and uid=" . $uid)->find();
            $x_arr = $uprofile->where("type=2 and uid=" . $uid)->find();
            $m_arr = $uprofile->where("type=3 and uid=" . $uid)->find();
            $f_arr = $uprofile->where("type=4 and uid=" . $uid)->find();
            $d_arr = $uprofile->where("type=5 and uid=" . $uid)->find();
            $y_arr = $uprofile->where("type=6 and uid=" . $uid)->find();
            $q_arr = $uprofile->where("type=7 and uid=" . $uid)->find();
            $obj = M("pro_color");
            $datas = $obj->select();
            $this->assign("phone", $phone);
            $this->assign("datas", $datas);
            $this->assign("arr", $arr);
            $this->assign("c_arr", $c_arr);
            $this->assign("x_arr", $x_arr);
            $this->assign("m_arr", $m_arr);
            $this->assign("f_arr", $f_arr);
            $this->assign("d_arr", $d_arr);
            $this->assign("y_arr", $y_arr);
            $this->assign("q_arr", $q_arr);
            $this->assign("belong", $belong);

            if (isMobile()) {
                $this->display("lt_m");
            } else {
                $this->display();
            }
        } else {
            $this->success("请派单", "../../../Order/orderList");
        }
    }

//编辑预约用户量体信息
    public function userAdd($type)
    {
        $user = $_POST['user'];
        $phone = $_POST['phone'];
        $length = $_POST["length"];//衣长
        $neckline = $_POST["neckline"];//领围
        $shoulder = $_POST["shoulder"];//肩宽
        $sleeve_long = $_POST["sleeve_long"];//袖长
        $sleeve_width = $_POST["sleeve_width"];//袖肥
        $cuff = $_POST["cuff"];//袖口
        $hipline = $_POST["hipline"];//臀围
        $chest = $_POST["chest"];//胸围
        $waistline = $_POST["waistline"];//腰围
        $mid_waist = $_POST["mid_waist"];//中腰
        $lts = $_POST["lts"];
        $descr = $_POST["descr"];
        $lower_hem = $_POST["lower_hem"];//下摆
        $rise = $_POST["rise"];//立裆
        $outside_len = $_POST["outside_len"];//裤长
        $tong_fork = $_POST["tong_fork"];//通裆
        $barre = $_POST["barre"];//横裆
        $leg_width = $_POST["leg_width"];//中裆
        $leg_open = $_POST["leg_open"];//脚口
        $skirt_len = $_POST["skirt_len"];//裙长
        if (isset($phone) && isset($lts)) {
            $timestamp = time();
            //主表
            $obj = M("user_main");
            $arr = $obj->where("phone=" . $phone)->find();
            $uid = $arr["uid"];
            $data["name"] = $user;
            $data["updated"] = $timestamp;
            $obj->where("phone=" . $phone)->save($data);
            //附表
            $profile = M("user_profile");
            $p_arr = $profile->where("type=" . $type . " and uid=" . $uid)->find();
            $p["neckline"] = $neckline;
            $p["shoulder"] = $shoulder;
            $p["sleeve_long"] = $sleeve_long;
            $p["sleeve_width"] = $sleeve_width;
            $p["cuff"] = $cuff;
            $p["hipline"] = $hipline;
            $p["chest"] = $chest;
            $p["waistline"] = $waistline;
            $p["length"] = $length;
            $p["lts"] = $lts;
            $p["mid_waist"] = $mid_waist;
            $p["descr"] = $descr;
            $p["skirt_len"] = $skirt_len;
            $p["leg_open"] = $leg_open;
            $p["leg_width"] = $leg_width;
            $p["barre"] = $barre;
            $p["lower_hem"] = $lower_hem;
            $p["outside_len"] = $outside_len;
            $p["rise"] = $rise;
            $p["tong_fork"] = $tong_fork;
            $p["updated"] = $timestamp;
            $p["type"] = $type;
            if (count($p_arr) == 0) {
                $p["uid"] = $uid;
                $profile->add($p);
            } else {
                $profile->where("type=" . $type . " and uid=" . $uid)->save($p);
            }
            $this->assign("phone", $phone);
            $this->assign("lts", $lts);
            $this->display("Order/orderEdit");
        } else {
            $this->error("提交失败");
        }
    }

    //添加新用户页面
    public function lt2()
    {
        if (isMobile()) {
            $this->display("lt2_m");
        } else {
            $this->display();
        }
    }

    //添加新客户
    public function userAdd2($type)
    {
        $user1 = session("user");
        if ($user1 == null || $user1 == '') {
            $this->display("User/index");
        }
        $user = $_POST['user'];
        $phone = $_POST['phone'];
        $addr = $_POST["addr"];
        //量体数据
        $length = $_POST["length"];//衣长
        $outside_len = $_POST["outside_len"];//裤长
        $chest = $_POST["chest"];//胸围
        $skirt_len = $_POST["skirt_len"];//裙长
        $mid_waist = $_POST["mid_waist"];//中腰
        $waistline = $_POST["waistline"];//腰围
        $lower_hem = $_POST["lower_hem"];//下摆
        $hipline = $_POST["hipline"];//臀围
        $shoulder = $_POST["shoulder"];//肩宽
        $rise = $_POST["rise"];//立裆
        $sleeve_long = $_POST["sleeve_long"];//袖长
        $tong_fork = $_POST["tong_fork"];//通裆
        $sleeve_width = $_POST["sleeve_width"];//袖肥
        $barre = $_POST["barre"];//横裆
        $cuff = $_POST["cuff"];//袖口
        $leg_width = $_POST["leg_width"];//中裆
        $neckline = $_POST["neckline"];//领围
        $leg_open = $_POST["leg_open"];//脚口
        $lts = $_POST["lts"];
        $descr = $_POST["descr"];
        if (isset($user) && isset($phone) && isset($lts)) {
            $timestamp = time();
            //主表
            $obj = M("user_main");
            $profile = M("user_profile");
            $arr = $obj->where("phone=" . $phone)->find();
            $shj = $obj->where("name='" . $user1 . "'")->find();
            if (empty($arr)) {
                $data["name"] = $user;
                $data["phone"] = $phone;
                $data["address"] = $addr;
                $data["updated"] = $timestamp;
                $data['rel'] = $shj['uid'];
                $uid = $obj->add($data);

                $data2["uid"] = $uid;
                $data2["type"] = $type;
                $profile->add($data2);
            } else {
                $uid = $arr["uid"];
            }
            //附表
            $p["neckline"] = $neckline;
            $p["shoulder"] = $shoulder;
            $p["sleeve_long"] = $sleeve_long;
            $p["sleeve_width"] = $sleeve_width;
            $p["cuff"] = $cuff;
            $p["hipline"] = $hipline;
            $p["chest"] = $chest;
            $p["waistline"] = $waistline;
            $p["length"] = $length;
            $p["lts"] = $lts;
            $p["mid_waist"] = $mid_waist;
            $p["descr"] = $descr;
            $p["skirt_len"] = $skirt_len;
            $p["leg_open"] = $leg_open;
            $p["leg_width"] = $leg_width;
            $p["barre"] = $barre;
            $p["lower_hem"] = $lower_hem;
            $p["outside_len"] = $outside_len;
            $p["rise"] = $rise;
            $p["tong_fork"] = $tong_fork;
            $p["updated"] = $timestamp;
            $p["type"] = $type;
            $arr2 = $profile->where("uid=" . $uid . " and type=" . $type)->find();
            if (count($arr2) == 0) {
                $p["uid"] = $uid;
                $profile->add($p);
            } else {
                $profile->where("uid=" . $uid . " and type=" . $type)->save($p);
            }
            $this->assign("phone", $phone);
            $this->assign("lts", $lts);

            if (isMobile()) {
                $this->display("Order/orderEdit_m");
            } else {
                $this->display("Order/orderEdit");
            }
        } else {
            $this->error("提交失败");
        }
    }

    //添加员工账号页面
    public function create()
    {
        $role = M("user_role");
        $datas = $role->select();

        $this->assign("datas", $datas);
        if (isMobile()) {
            $this->display("create_m");
        } else {
            $this->display();
        }
    }

    //添加员工账号
    public function create2()
    {
        $user = $_POST["user"];
        $pwd = $_POST["pwd"];
        $phone = $_POST["phone"];
        $role = $_POST["select"];
        $intro = $_POST["intro"];
        if (isset($user) && isset($pwd)) {
            $umain = M("user_main");
            $data["name"] = $user;
            $data["pwd"] = $pwd;
            $data["phone"] = $phone;
            $data["role"] = $role;
            $data["intro"] = $intro;
            $uid = $umain->add($data);
            if (!empty($uid)) {
                $desc = "账号添加成功！";
                $this->assign("desc", $desc);
                if (isMobile()) {
                    $this->display("create_m");
                } else {
                    $this->display("create");
                }
            } else {
                $desc = "账号添加失败！";
                $this->assign("desc", $desc);
                if (isMobile()) {
                    $this->display("create_m");
                } else {
                    $this->display("create");
                }
            }
        }
    }

    //客户列表
    public function userList()
    {
        //判断session是否存在
        $user = session("user");
        if ($user == null || $user == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
            } else {
                $this->display("User/index");
            }
        } else {
            $obj = M("user_main");
            $role = session("role");
            $arr = $obj->where("name='" . $user . "'")->find();
            if ($arr['role'] != 0) {
                $count = $obj->where("role=1 and rel=" . $arr['uid'])->count();
                $p = getPage($count);
                $list = $obj->where("role=1 and rel=" . $arr['uid'])->order("updated desc")->limit($p->firstRow, $p->listRows)->select();
            } else {
                $count = $obj->where("role=1")->count();
                $p = getPage($count);
                $list = $obj->where("role=1")->order("updated desc")->limit($p->firstRow, $p->listRows)->select();
            }

            $this->assign('datas', $list);
            $this->assign("role", $role);
            $this->assign('page', $p->show());

            if (isMobile()) {
                $this->display("userList_m");
            } else {
                $this->display();
            }
        }
    }

    //量体信息
    public function ltInfo($id)
    {
        $obj = M("user_profile");
        $shirt = $obj->where("type =1 and uid=" . $id)->find();
        $suit = $obj->where("type =2 and uid=" . $id)->find();
        $mj = $obj->where("type =3 and uid=" . $id)->find();
        $fy = $obj->where("type =4 and uid=" . $id)->find();
        $dy = $obj->where("type =5 and uid=" . $id)->find();
        $yrs = $obj->where("type =6 and uid=" . $id)->find();
        $skirt = $obj->where("type =7 and uid=" . $id)->find();
        $obj2 = M("user_main");
        $data2 = $obj2->where("uid=" . $id)->find();
        $this->assign("data2", $data2);
        $this->assign("shirt", $shirt);
        $this->assign("suit", $suit);
        $this->assign("mj", $mj);
        $this->assign("fy", $fy);
        $this->assign("dy", $dy);
        $this->assign("yrs", $yrs);
        $this->assign("skirt", $skirt);
        if (isMobile()) {
            $this->display("ltInfo_m");
        } else {
            $this->display();
        }
    }

    //每个客户的订单信息
    public function orderInfo($id)
    {
        $obj = M("order_main");
        $count = $obj->where("uid=" . $id)->count();
        $p = getPage($count);
        $list = $obj->where("uid=" . $id)->limit($p->firstRow, $p->listRows)->select();
        $datas = [];
        foreach ($list as $l) {
            $uid = $l["uid"];
            $obj2 = M("user_main");
            $arr = $obj2->where("uid=" . $uid)->find();
            $data["name"] = $arr["name"];
            $data["order_time"] = $l["order_time"];
            $data["color"] = $l["color"];
            $data["collar_type"] = $l["collar_type"];
            $data["sleeve_type"] = $l["sleeve_type"];
            $data["type"] = $l["type"];
            $data["status"] = $l["status"];
            $data["omid"] = $l["omid"];
            $datas[] = $data;
        }
        $this->assign('datas', $datas);
        $this->assign("page", $p->show());

        if (isMobile()) {
            $this->display("Order/orderList2_m");
        } else {
            $this->display("Order/orderList2");
        }
    }

    //员工列表
    public function userList2()
    {
        $obj = M("user_main");
        $count = $obj->where("role!=1")->count();
        $p = getPage($count);
        $list = $obj->where("role!=1")->limit($p->firstRow, $p->listRows)->select();
        $this->assign('datas', $list);
        $this->assign('page', $p->show());
        if (isMobile()) {
            $this->display("userList2_m");
        } else {
            $this->display("userList2");
        }
    }

    //员工删除
    public function userDel2($id)
    {
        $obj = M("user_main");
        $data = $obj->where("uid=" . $id)->delete();
        if ($data == false || $data == 0) {
            $this->error('删除失败', '../../userList2');
        } else {
            $this->success('删除成功', '../../userList2');
        }
    }

    //登出
    public function loginOut()
    {
        if (isset($_COOKIE[session("user")])) {
            setcookie(session("user"), '', time() - 1, "/");
        }
        session('[destroy]');
        $this->redirect("User/index");
    }

    //客服添加预约页面
    public function yuyueAdd()
    {
        $obj = M("user_main");
        $datas = $obj->where("role=5")->select();
        $this->assign("datas", $datas);
        if (isMobile()) {
            $this->display("yuyueAdd_m");
        } else {
            $this->display();
        }
    }

    //客服添加预约
    public function yuyueAdd2()
    {
        $phone = $_POST["phone"];
        $addr = $_POST["addr"];
        $yuyue_date = $_POST["yuyue_date"];
        $yuyue = $_POST["yuyue"];
        $descr = $_POST["descr"];
        $xin = $_POST["xin"];
        $company = $_POST["company"];
        $sale = I("post.sale");
        $obj = M("user_record");
        $obj2 = M("user_main");
        $obj3 = M("user_profile");
        $rs = $obj2->where("phone=" . $phone)->find();
        $timestamp = time();
        if (empty($rs)) {
            $data["phone"] = $phone;
            $data["address"] = $addr;
            $data["company"] = $company;
            $data["sale"] = $sale;
            $uid = $obj2->add($data);
            $data2["uid"] = $uid;
            $obj3->add($data2);
            $arr["uid"] = $uid;
            $arr["phone"] = $phone;
            $arr["addr"] = $addr;
            $arr["yuyue"] = $yuyue;
            $arr["yuyue_date"] = $yuyue_date;
            $arr["descr"] = $descr;
            $arr["updated"] = $timestamp;
            $arr["xin"] = $xin;
            $arr["company"] = $company;
            $arr["source"] = 3;
            $arr["sale"] = $sale;
            $id = $obj->add($arr);
            if (empty($id)) {
                $this->error('添加失败', '../Order/orderList');
            } else {
                $this->success('添加成功', '../Order/orderList');
            }
        } else {
            $uid = $rs["uid"];
            $arr["uid"] = $uid;
            $arr["phone"] = $phone;
            $arr["addr"] = $addr;
            $arr["yuyue"] = $yuyue;
            $arr["yuyue_date"] = $yuyue_date;
            $arr["descr"] = $descr;
            $arr["updated"] = $timestamp;
            $arr["xin"] = $xin;
            $arr["company"] = $company;
            $arr["source"] = 3;
            $arr["sale"] = $sale;
            $id = $obj->add($arr);
            $obj2->address = $addr;
            $obj2->updated = $timestamp;
            $obj2->company = $company;
            $obj2->sale = $sale;
            $obj2->where("uid=" . $uid)->save();
            if (empty($id)) {
                $this->error('添加失败', '../Order/orderList');
            } else {
                $this->success('添加成功', '../Order/orderList');
            }
        }
    }

    //编辑预约页面
    public function yuyueEdit($id)
    {
        $obj = M("user_record");
        $obj2 = M("user_main");
        $arr = $obj->where("id=" . $id)->find();
        $mainArr = $obj2->where("phone=" . $arr["phone"])->find();
        $arr2 = $obj2->where("role=5")->select();
        $this->assign("datas", $arr2);
        $this->assign("data", $arr);
        $this->assign("uid", $mainArr["uid"]);

        if (isMobile()) {
            $this->display("yuyueEdit_m");
        } else {
            $this->display();
        }
    }

    //编辑预约
    public function yuyueEdit2()
    {
        $data = I("post.");
        $obj = M("user_record");
        $obj2 = M("user_main");
        $rs = $obj->where("id=" . $data["id"])->save($data);
        $obj2->phone = $data["phone"];
        $rs2 = $obj->where("uid=" . $data["uid"])->save();
        if ($rs == false) {
            $this->error('修改失败', '../Order/orderList');
        } else {
            redirect("../Order/orderList");
        }
    }

    //量体信息编辑
    public function ltInfoEdit($type, $id)
    {
        $length = $_POST["length"];//衣长
        $neckline = $_POST["neckline"];//领围
        $shoulder = $_POST["shoulder"];//肩宽
        $sleeve_long = $_POST["sleeve_long"];//袖长
        $sleeve_width = $_POST["sleeve_width"];//袖肥
        $cuff = $_POST["cuff"];//袖口
        $hipline = $_POST["hipline"];//臀围
        $chest = $_POST["chest"];//胸围
        $waistline = $_POST["waistline"];//腰围
        $mid_waist = $_POST["mid_waist"];//中腰
        $lts = $_POST["lts"];
        $descr = $_POST["descr"];
        $lower_hem = $_POST["lower_hem"];//下摆
        $rise = $_POST["rise"];//立裆
        $outside_len = $_POST["outside_len"];//裤长
        $tong_fork = $_POST["tong_fork"];//通裆
        $barre = $_POST["barre"];//横裆
        $leg_width = $_POST["leg_width"];//中裆
        $leg_open = $_POST["leg_open"];//脚口
        $skirt_len = $_POST["skirt_len"];//裙长
        if (isset($lts)) {
            //附表
            $profile = M("user_profile");
            $data["neckline"] = $neckline;
            $data["shoulder"] = $shoulder;
            $data["sleeve_long"] = $sleeve_long;
            $data["sleeve_width"] = $sleeve_width;
            $data["cuff"] = $cuff;
            $data["hipline"] = $hipline;
            $data["chest"] = $chest;
            $data["waistline"] = $waistline;
            $data["length"] = $length;
            $data["lts"] = $lts;
            $data["mid_waist"] = $mid_waist;
            $data["descr"] = $descr;
            $data["skirt_len"] = $skirt_len;
            $data["leg_open"] = $leg_open;
            $data["leg_width"] = $leg_width;
            $data["barre"] = $barre;
            $data["lower_hem"] = $lower_hem;
            $data["outside_len"] = $outside_len;
            $data["rise"] = $rise;
            $data["tong_fork"] = $tong_fork;
            $data["type"] = $type;
            $rs = $profile->where("type=" . $type . " and uid=" . $id)->find();
            if (empty($rs)) {
                $data["uid"] = $id;
                $profile->add($data);
            } else {
                $rs2 = $profile->where("type=" . $type . " and uid=" . $id)->save($data);
            }
            $obj = M("user_main");
            $obj->belong = $lts;
            $obj->where("uid=" . $id)->save();
            $this->success("提交成功", "../../../../ltInfo/id/" . $id);
        } else {
            $this->error("提交失败");
        }
    }

    //编辑客户信息页面
    public function userEdit($id)
    {
        $obj = M("user_main");
        $arr = $obj->where("uid=" . $id)->find();
        $this->assign("data", $arr);
        if (isMobile()) {
            $this->display("userEdit_m");
        } else {
            $this->display();
        }
    }

    //编辑客户信息
    public function userEdit2()
    {
        $data["name"] = $_POST["name"];
        $data["phone"] = $_POST["phone"];
        $data["address"] = $_POST["address"];
        $data["weixin"] = $_POST["weixin"];
        $data["intro"] = $_POST["intro"];
        $uid = $_POST["uid"];
        $obj = M("user_main");
        $rs = $obj->where("uid=" . $uid)->save($data);
        if ($rs == false) {
            $this->error('修改失败', 'userEdit');
        } else {
            $this->success('修改成功', 'userList');
        }
    }

    //客户删除
    public function userDel($id)
    {
        $obj = M("user_main");
        $rs = $obj->where("uid=" . $id)->delete();
        if ($rs == false || $rs == 0) {
            $this->error("删除失败", "../../userList");
        } else {
            $this->success("删除成功", '../../userList');
        }
    }

    //ajax 量体信息
    public function check_user($phone)
    {
        echo 2;
    }

    //客户地址修改
    public function addrEdit()
    {
        $addr = $_POST["addr"];
        $uid = $_POST["uid"];
        $obj = M("user_main");
        $obj->address = $addr;
        $rs = $obj->where("uid=" . $uid)->save();
        if ($rs == false || $rs == 0) {
            $this->error("修改失败", "ltInfo/id/" . $uid);
        } else {
            $this->success("修改成功", "ltInfo/id/" . $uid);
        }
    }

    //查询是否是我们已有的客户
    public function readUser()
    {
        $phone = $_POST["phone"];
        $obj = M("user_main");
        $arr = $obj->where("phone=" . $phone)->find();
        if (empty($arr)) {
            $desc = "没有数据，请填写新客户数据";
            $this->assign("desc", $desc);
            $this->display("lt2");
        } else {
            $uid = $arr["uid"];
            $user_arr["name"] = $arr["name"];
            $user_arr["addr"] = $arr["address"];
            $user_arr["phone"] = $phone;
            $this->assign("user_arr", $user_arr);
            //量体信息
            $obj2 = M("user_profile");
            $shirt_arr = $obj2->where("type=1 and uid=" . $uid)->find();//衬衫
            $suit_arr = $obj2->where("type=2 and uid=" . $uid)->find();//西服
            $vest_arr = $obj2->where("type=3 and uid=" . $uid)->find();//马甲
            $fy_arr = $obj2->where("type=4 and uid=" . $uid)->find();//风衣
            $dy_arr = $obj2->where("type=5 and uid=" . $uid)->find();//大衣
            $yrs_arr = $obj2->where("type=6 and uid=" . $uid)->find();//羊绒衫
            $skirt_arr = $obj2->where("type=7 and uid=" . $uid)->find();//裙子
            $this->assign("shirt_arr", $shirt_arr);
            $this->assign("suit_arr", $suit_arr);
            $this->assign("vest_arr", $vest_arr);
            $this->assign("fy_arr", $fy_arr);//4
            $this->assign("dy_arr", $dy_arr);
            $this->assign("yrs_arr", $yrs_arr);
            $this->assign("skirt_arr", $skirt_arr);

            if (isMobile()) {
                $this->display("lt2_m");
            } else {
                $this->display("lt2");
            }
        }
    }

    //客户添加新用户页面
    public function kh_lt()
    {
        if (isMobile()) {
            $this->display("kh_lt_m");
        } else {
            $this->display();
        }
    }

    //客户列表添加新客户
    public function kh_lt2($type)
    {
        $user1 = session("user");
        if ($user1 == null || $user1 == '') {
            $this->display("User/index");
        }
        $user = $_POST['user'];
        $phone = $_POST['phone'];
        $addr = $_POST["addr"];
        $company = $_POST["company"];
        $sex = $_POST["sex"];
        //量体数据
        $length = $_POST["length"];//衣长
        $outside_len = $_POST["outside_len"];//裤长
        $chest = $_POST["chest"];//胸围
        $skirt_len = $_POST["skirt_len"];//裙长
        $mid_waist = $_POST["mid_waist"];//中腰
        $waistline = $_POST["waistline"];//腰围
        $lower_hem = $_POST["lower_hem"];//下摆
        $hipline = $_POST["hipline"];//臀围
        $shoulder = $_POST["shoulder"];//肩宽
        $rise = $_POST["rise"];//立裆
        $sleeve_long = $_POST["sleeve_long"];//袖长
        $tong_fork = $_POST["tong_fork"];//通裆
        $sleeve_width = $_POST["sleeve_width"];//袖肥
        $barre = $_POST["barre"];//横裆
        $cuff = $_POST["cuff"];//袖口
        $leg_width = $_POST["leg_width"];//中裆
        $neckline = $_POST["neckline"];//领围
        $leg_open = $_POST["leg_open"];//脚口
        $lts = $_POST["lts"];
        $descr = $_POST["descr"];
        if (isset($user) && isset($phone) && isset($lts)) {
            $timestamp = time();
            //主表
            $obj = M("user_main");
            $profile = M("user_profile");
            $arr = $obj->where("phone=" . $phone)->find();
            $shj = $obj->where("name='" . $user1 . "'")->find();
            if (empty($arr)) {
                $data["name"] = $user;
                $data["phone"] = $phone;
                $data["address"] = $addr;
                $data["company"] = $company;
                $data["sex"] = $sex;
                $data["updated"] = $timestamp;
                $data["rel"] = $shj['uid'];
                $uid = $obj->add($data);

                $data2["uid"] = $uid;
                $data2["type"] = $type;
                $profile->add($data2);
            } else {
                $uid = $arr["uid"];
            }
            //附表
            $p["neckline"] = $neckline;
            $p["shoulder"] = $shoulder;
            $p["sleeve_long"] = $sleeve_long;
            $p["sleeve_width"] = $sleeve_width;
            $p["cuff"] = $cuff;
            $p["hipline"] = $hipline;
            $p["chest"] = $chest;
            $p["waistline"] = $waistline;
            $p["length"] = $length;
            $p["lts"] = $lts;
            $p["mid_waist"] = $mid_waist;
            $p["descr"] = $descr;
            $p["skirt_len"] = $skirt_len;
            $p["leg_open"] = $leg_open;
            $p["leg_width"] = $leg_width;
            $p["barre"] = $barre;
            $p["lower_hem"] = $lower_hem;
            $p["outside_len"] = $outside_len;
            $p["rise"] = $rise;
            $p["tong_fork"] = $tong_fork;
            $p["updated"] = $timestamp;
            $p["type"] = $type;
            $arr2 = $profile->where("uid=" . $uid . " and type=" . $type)->find();
            if (count($arr2) == 0) {
                $p["uid"] = $uid;
                $profile->add($p);
            } else {
                $profile->where("uid=" . $uid . " and type=" . $type)->save($p);
            }
            redirect("../../userList");
        } else {
            $this->error("提交失败");
        }
    }

    //查询是否是我们已有的客户
    public function readUser2()
    {
        $phone = $_POST["phone"];
        $obj = M("user_main");
        $arr = $obj->where("phone=" . $phone)->find();
        if (empty($arr)) {
            $desc = "没有数据，请填写新客户数据";
            $this->assign("desc", $desc);
            $this->display("kh_lt");
        } else {
            $uid = $arr["uid"];
            $user_arr["name"] = $arr["name"];
            $user_arr["addr"] = $arr["address"];
            $user_arr["phone"] = $phone;
            $user_arr["company"] = $arr["company"];
            $user_arr["sex"] = $arr["sex"];
            $this->assign("user_arr", $user_arr);
            //量体信息
            $obj2 = M("user_profile");
            $shirt_arr = $obj2->where("type=1 and uid=" . $uid)->find();//衬衫
            $suit_arr = $obj2->where("type=2 and uid=" . $uid)->find();//西服
            $vest_arr = $obj2->where("type=3 and uid=" . $uid)->find();//马甲
            $fy_arr = $obj2->where("type=4 and uid=" . $uid)->find();//风衣
            $dy_arr = $obj2->where("type=5 and uid=" . $uid)->find();//大衣
            $yrs_arr = $obj2->where("type=6 and uid=" . $uid)->find();//羊绒衫
            $skirt_arr = $obj2->where("type=7 and uid=" . $uid)->find();//裙子
            $this->assign("shirt_arr", $shirt_arr);
            $this->assign("suit_arr", $suit_arr);
            $this->assign("vest_arr", $vest_arr);
            $this->assign("fy_arr", $fy_arr);//4
            $this->assign("dy_arr", $dy_arr);
            $this->assign("yrs_arr", $yrs_arr);
            $this->assign("skirt_arr", $skirt_arr);

            if (isMobile()) {
                $this->display("kh_lt_m");
            } else {
                $this->display("kh_lt");
            }
        }
    }

    //ajax验证创建账号是否存在
    public function checkUser()
    {
        $data = I("post.");
        $user = M("user_main");
        $arr = $user->where("name='" . $data["name"] . "'")->find();
        if (!empty($arr)) {
            $res['status'] = 1;//如果不为空，则用户已存在
        }
        $res2 = json_encode($res);
        $this->ajaxReturn($res2, 'json');
    }

    /*
     * 商家个人中心
     * @wxj
     * */
    public function shjCenter()
    {
        $this->display();
    }

    /*
     * 商家注册账号
     * @wan
     * */
    public function shopReg()
    {
        $this->display();
    }

    /*
     * 商家注册post提交
     * @wan
     * */
    public function shopRegPost()
    {
        if (!IS_AJAX) {
            $this->error("非法请求");
        } else {
            $phone = $_POST['phone'];
            $pwd = $_POST['pwd'];
            $role = $_POST['role'];
            $user = M("user_main");
            $res = $user->where("phone=" . $phone . " and role=" . $role)->find();
            if (!empty($res)) {
                $arr['status'] = 1002;//您已是商家
                $this->ajaxReturn($arr);
            }


            $data['phone'] = $phone;
            $data['pwd'] = $pwd;
            $data['role'] = $role;
            $data['updated'] = NOW_TIME;
            $res = $user->where("phone=" . $phone)->find();
            if (!empty($res)) {//如果手机号以存在
                $user->where("phone=" . $phone)->save($data);
                $uid = $res['uid'];
            } else {
                $uid = $user->add($data);
            }
            if ($uid > 0) {
                session("user", $phone);
                session("role", $role);
                session("uid", $uid);
                cookie("user", $phone);
                cookie("user", $phone, 60);
                $arr['status'] = 1001;//注册成功
                $this->ajaxReturn($arr);
            } else {
                $arr['status'] = 1003;//注册失败
                $this->ajaxReturn($arr);
            }
        }
    }

    /*
     * 商家忘记密码
     * ＠wan
     *
     * */
    public function forgetPwd()
    {
        $this->display();
    }

    /*
     * 商家忘记密码提交
     * ＠wan
     * */
    public function forgetPwdPost()
    {
        if (!IS_AJAX) {
            $this->error("非法请求");
        } else {
            $phone = $_POST['phone'];
            $pwd = $_POST['pwd'];
            $role = $_POST['role'];
            $user = M("user_main");
            $res = $user->where("phone=" . $phone . " and role=" . $role)->find();
            if (empty($res)) {
                $arr['code'] = 1002;//您还不是商家，请注册
                $this->ajaxReturn($arr);
            }

            $data['phone'] = $phone;
            $data['pwd'] = $pwd;
            $data['role'] = $role;
            $data['updated'] = NOW_TIME;
            $res = $user->where("phone=" . $phone)->find();
            if (!empty($res)) {//如果手机号以存在
                $user->where("phone=" . $phone)->save($data);
                $uid = $res['uid'];
                if ($uid > 0) {
                    $arr['code'] = 1001;//修改成功
                    $this->ajaxReturn($arr);
                } else {
                    $arr['code'] = 1004;//修改失败
                    $this->ajaxReturn($arr);
                }
            } else {
                $arr['code'] = 1003;//没有账户，请注册
                $this->ajaxReturn($arr);
            }
        }
    }
}